package DAO;

import Entities.UserEntity;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import java.util.List;

public class UserDao extends HibernateDaoSupport
{
    //添加用户记录（注册时调用）
    public boolean add(UserEntity ue)
    {
        String user_name = ue.getUserName();
        boolean flag = false;
        List<UserEntity> userList = (List<UserEntity>) this.getHibernateTemplate().find("from UserEntity u where u.userName=?",user_name);
        if(userList.size()==0)
        {
            //若数据库user表中没有存在该账号，则注册成功
            this.getHibernateTemplate().save(ue);
            flag = true;
        }
        return flag;
    }

    //只更新用户标签，更新成功返回true
    public boolean updateTag(UserEntity ue)
    {
        String user_name = ue.getUserName();
        String userTag1 = ue.getUserTag1();
        String userTag2 = ue.getUserTag2();
        String userTag3 = ue.getUserTag3();
        String userTag4 = ue.getUserTag4();
        String userTag5 = ue.getUserTag5();
        String userTag6 = ue.getUserTag6();
        List<UserEntity> userList = (List<UserEntity>) this.getHibernateTemplate().find("from UserEntity u where u.userName=?",user_name);

        if(userList.size()!=1)
            return false;

        UserEntity foundUserEntity = userList.get(0);

        foundUserEntity.setUserTag1(userTag1);
        foundUserEntity.setUserTag2(userTag2);
        foundUserEntity.setUserTag3(userTag3);
        foundUserEntity.setUserTag4(userTag4);
        foundUserEntity.setUserTag5(userTag5);
        foundUserEntity.setUserTag6(userTag6);
        this.getHibernateTemplate().update(foundUserEntity);
        return true;
    }

    //返回用户信息（用于修改用户信息界面）
    public UserEntity returnUserInfo(String user_name)
    {
        List<UserEntity> userList = (List<UserEntity>) this.getHibernateTemplate().find("from UserEntity u where u.userName=?",user_name);
        if(userList.size()!=1)
            return null;
        else
        {
            UserEntity foundUserEntity = userList.get(0);

            //不能直接对fondUserEntiy进行属性修改，否则会直接影响数据库中的内容
            UserEntity returnEntity = new UserEntity();
            returnEntity.setUserName(foundUserEntity.getUserName());
            returnEntity.setUserTag1(foundUserEntity.getUserTag1());
            returnEntity.setUserTag2(foundUserEntity.getUserTag2());
            returnEntity.setUserTag3(foundUserEntity.getUserTag3());
            returnEntity.setUserTag4(foundUserEntity.getUserTag4());
            returnEntity.setUserTag5(foundUserEntity.getUserTag5());
            returnEntity.setUserTag6(foundUserEntity.getUserTag6());
            return returnEntity;
        }
    }

    //修改用户密码
    public boolean updatePassword(String user_name, String user_password)
    {
        //传过来的密码肯定不为空，因为如果是空的在Action中就被拦截了
        boolean flag = false;
        List<UserEntity> userList = (List<UserEntity>) this.getHibernateTemplate().find("from UserEntity u where u.userName=?",user_name);
        if(userList.size()!=1)
            flag=false;
        else
        {
            UserEntity foundUserEntity = userList.get(0);
            foundUserEntity.setUserPassword(user_password);
            this.getHibernateTemplate().update(foundUserEntity);
            flag = true;
        }
        return flag;
    }

    //（在用户信息页面）更新用户信息
    public boolean update(UserEntity ue)
    {
        String user_name = ue.getUserName();
        String user_password = ue.getUserPassword();
        String userTag1 = ue.getUserTag1();
        String userTag2 = ue.getUserTag2();
        String userTag3 = ue.getUserTag3();
        String userTag4 = ue.getUserTag4();
        String userTag5 = ue.getUserTag5();
        String userTag6 = ue.getUserTag6();
        List<UserEntity> userList = (List<UserEntity>) this.getHibernateTemplate().find("from UserEntity u where u.userName=?",user_name);

        if(userList.size()!=1)
            return false;

        UserEntity foundUserEntity = userList.get(0);

        if(!user_password.isEmpty())
            foundUserEntity.setUserPassword(user_password);
        if(!userTag1.isEmpty())
            foundUserEntity.setUserTag1(userTag1);
        if(!userTag2.isEmpty())
            foundUserEntity.setUserTag2(userTag2);
        if(!userTag3.isEmpty())
            foundUserEntity.setUserTag3(userTag3);
        if(!userTag4.isEmpty())
            foundUserEntity.setUserTag4(userTag4);
        if(!userTag5.isEmpty())
            foundUserEntity.setUserTag5(userTag5);
        if(!userTag6.isEmpty())
            foundUserEntity.setUserTag6(userTag6);
        this.getHibernateTemplate().update(foundUserEntity);
        return true;
    }

    //删除用户记录
    public void delete(UserEntity u)
    {
        getHibernateTemplate().delete(u);
    }

    //根据用户名查找用户记录
    public UserEntity getUserByUsername(String username)
    {
        UserEntity u = (UserEntity) getHibernateTemplate().get(UserEntity.class, username);
        return u;
    }

    //普通用户登陆或注册验证方法,若用户已存在则返回true
    //loginOrRegister为1表示登陆验证，为2表示注册验证
    @SuppressWarnings("unchecked")
    public boolean checkUser(String user_name, String user_password, int loginOrRegister)
    {
        boolean flag = false;
//        String hql = "from UserEntity u where u.userName= '"+user_name+"' and u.userPassword='"+user_password+"'";

        UserEntity userEntity = new UserEntity();
        userEntity.setUserName(user_name);
        userEntity.setUserPassword(user_password);
        //执行hql语句
        List<UserEntity> userList = (List<UserEntity>) this.getHibernateTemplate().find("from UserEntity u where u.userName=?",user_name);

        if(loginOrRegister == 1)   //登陆验证
        {
            if (userList.size() == 1)
            {
                if (user_password.equals(userList.get(0).getUserPassword()))
                    flag = true;
                else
                    flag = false;
            }
            return flag;  //该值为true则表示在数据库中找到相应记录
        }
        else if(loginOrRegister == 2)  //注册验证
        {
            if (userList.size() == 1)
                return true;
            else
                return false;  //该值为true则表示在数据库中找到相应记录
        }
        else  //错误情况：loginOrRegister的值非法
            return false;
    }


}
